<template>
    <div class="pagebody">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 订单清账 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="search-box">
                <el-form :inline="true">
                    <el-form-item label="代理商" prop="agentid">
                        <el-select v-model="searchSet.agentid" class="formInput" placeholder="请选择代理商" clearable>
                            <el-option
                                v-for="(item, index) in agentList"
                                :key="index"
                                :label="`${item.name}(${item.contact})`"
                                :value="item.id"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="内单号">
                        <el-input v-model="searchSet.code" prefix-icon="el-icon-search" clearable placeholder="请输入搜索信息"></el-input>
                    </el-form-item>

                    <el-form-item label="发货起止时间">
                        <el-date-picker
                            v-model="searchSet.startTime"
                            clearable
                            type="datetime"
                            placeholder="选择日期时间"
                            align="right"
                            :picker-options="pickerTimeOptions"
                        >
                        </el-date-picker>
                        &nbsp; <label for="">~</label> &nbsp;
                        <el-date-picker
                            v-model="searchSet.endTime"
                            clearable
                            type="datetime"
                            placeholder="选择日期时间"
                            align="right"
                            :picker-options="pickerTimeOptions"
                        ></el-date-picker>
                    </el-form-item>
                    <el-form-item label="客户代码">
                        <el-input
                            v-model="searchSet.userCode"
                            prefix-icon="el-icon-search"
                            clearable
                            placeholder="请输入用户Code"
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="订单状态">
                        <el-select v-model="searchSet.orderStatus" placeholder="选择状态" clearable>
                            <el-option
                                v-for="item in orderStatusOption"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="用户支付状态">
                        <el-select v-model="searchSet.ispayed" placeholder="选择状态" clearable>
                            <el-option v-for="item in orderPayOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="代理商支付状态">
                        <el-select v-model="searchSet.isagentpayed" placeholder="选择状态" clearable>
                            <el-option
                                v-for="item in orderAgentPayOption"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="经手人">
                        <el-input
                            v-model="searchSet.sendUserName"
                            prefix-icon="el-icon-search"
                            clearable
                            placeholder="请输入搜索信息"
                        ></el-input>
                    </el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    <el-button type="primary" @click="batchAppuser">用户清账</el-button>
                    <el-button type="primary" @click="batchAgent">代理商清账</el-button>
                    <el-button type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
                </el-form>
            </div>
            <el-table
                :data="tableData"
                v-loading="tableLoading"
                border
                class="table"
                ref="mainTable"
                :row-class-name="tableRowClassName"
                @selection-change="selectionChange"
            >
                <el-table-column type="selection" width="55" fixed label="选择"></el-table-column>
                <el-table-column prop="appusercode" label="客户代码" align="center" />
                <el-table-column prop="channelname" label="发货渠道" align="center" width="130" />
                <el-table-column prop="nationname" label="收货国家" align="center" />
                <el-table-column prop="code" label="内单号" align="center" />
                <el-table-column prop="price" label="支付金额" align="center" width="80" />
                <el-table-column prop="paytime" label="用户支付时间" width="100" align="center" />
                <el-table-column label="用户支付状态" align="center">
                    <template slot-scope="scope">
                        <div
                            class="CustomTag"
                            :style="{
                                'background-color': orderPayOption[scope.row.ispayed] ? orderPayOption[scope.row.ispayed].color : '#909399'
                            }"
                        >
                            {{ orderPayOption[scope.row.ispayed] ? orderPayOption[scope.row.ispayed].label : '无效' }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="agentpaytime" label="代理支付时间" width="100" align="center" />
                <el-table-column label="代理支付状态" align="center">
                    <template slot-scope="scope">
                        <div
                            class="CustomTag"
                            :style="{
                                'background-color': orderAgentPayOption[scope.row.isagentpayed]
                                    ? orderAgentPayOption[scope.row.isagentpayed].color
                                    : '#909399'
                            }"
                        >
                            {{ orderAgentPayOption[scope.row.isagentpayed] ? orderAgentPayOption[scope.row.isagentpayed].label : '无效' }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="订单状态" align="center">
                    <template slot-scope="scope">
                        <div
                            class="CustomTag"
                            :style="{
                                'background-color': orderStatusOption[scope.row.orderstatus]
                                    ? orderStatusOption[scope.row.orderstatus].color
                                    : '#909399'
                            }"
                        >
                            {{ orderStatusOption[scope.row.orderstatus] ? orderStatusOption[scope.row.orderstatus].label : '无效' }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-s-order" @click="handleDetail(scope.$index, scope.row)">详情</el-button>
                        <el-button
                            v-if="Number(scope.row.orderstatus) == 3 && Number(scope.row.ispayed) == 0"
                            type="text"
                            icon="el-icon-s-claim"
                            @click="handlePay(scope.$index, scope.row)"
                            >用户清账</el-button
                        ><el-button
                            v-if="
                                (Number(scope.row.orderstatus) == 4 || Number(scope.row.orderstatus) == 5) &&
                                Number(scope.row.isagentpayed) == 0
                            "
                            type="text"
                            icon="el-icon-s-claim"
                            @click="handleAgentPay(scope.$index, scope.row)"
                            >代理商清账</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="sizes, total, prev, pager, next"
                    :current-page="query.index"
                    :page-sizes="[10, 20, 30, 40, 50, 100]"
                    :page-size="query.pageSize"
                    :total="itemTotal"
                    @current-change="handlePageChange"
                    @size-change="handlePageSizeChange"
                ></el-pagination>
            </div>
        </div>

        <el-dialog title="用户清账" v-dialogDrag center :visible.sync="payVisible" width="600px">
            <el-form ref="payform" size="mini" :model="payform" :rules="payformRules" label-width="200px">
                <el-descriptions :column="1" style="margin-bottom: 20px" border>
                    <el-descriptions-item label="内单号">
                        {{ payform.code }}
                    </el-descriptions-item>
                    <el-descriptions-item label="价格(美元)">${{ payform.price }} (￥{{ payform.targetprice }} )</el-descriptions-item>

                    <el-descriptions-item label="到付金额(美元)">${{ payform.freightprice }} </el-descriptions-item>
                    <el-descriptions-item label="用户编码">
                        {{ payform.usercode }}
                    </el-descriptions-item>
                    <el-descriptions-item label="用户姓名">
                        {{ payform.username }}
                    </el-descriptions-item>
                    <el-descriptions-item label="账号余额" :span="2">
                        <template v-if="payform.useraccounts && payform.useraccounts.length > 0">
                            <div v-for="(item, index) in payform.useraccounts" :key="index" class="FlexRowVCenter">
                                <div>{{ item.currencyname }}:</div>
                                <div>{{ item.amount }}</div>
                            </div>
                        </template>
                        <div v-else>暂无余额</div>
                    </el-descriptions-item>
                </el-descriptions>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="payVisible = false">取 消</el-button>
                <el-button type="primary" @click="surePay">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="代理商清账" v-dialogDrag center :visible.sync="payAgentVisible" width="600px">
            <el-form ref="payAgentform" size="mini" :model="payAgentform" :rules="payAgentformRules" label-width="200px">
                <el-descriptions :column="1" style="margin-bottom: 20px" border>
                    <el-descriptions-item label="内单号">
                        {{ payAgentform.code }}
                    </el-descriptions-item>
                    <el-descriptions-item label="代理商">
                        {{ payAgentform.agentname }}
                    </el-descriptions-item>
                    <el-descriptions-item label="成本价格(美元)"
                        >${{ payAgentform.agentprice }}(￥{{ payAgentform.localagentprice }})
                    </el-descriptions-item>

                    <el-descriptions-item label="账号余额" :span="2">
                        <template v-if="payAgentform.agentaccounts && payAgentform.agentaccounts.length > 0">
                            <div v-for="(item, index) in payAgentform.agentaccounts" :key="index" class="FlexRowVCenter">
                                <div>{{ item.currencyname }}:</div>
                                <div>{{ item.amount }}</div>
                            </div>
                        </template>
                        <div v-else>暂无余额</div>
                    </el-descriptions-item>
                </el-descriptions>
                <el-form-item label="货币种类" prop="currencyid">
                    <el-select v-model="payAgentform.currencyid" class="formInput" placeholder="选择货币种类" clearable>
                        <el-option
                            v-for="(item, index) in currencyList"
                            :key="index"
                            :label="`${item.name}(${item.code})`"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="金额" prop="amount">
                    <el-input
                        v-model="payAgentform.amount"
                        placeholder="请输入充值金额"
                        maxlength="10"
                        show-word-limit
                        clearable
                        class="formInput"
                    ></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="payAgentVisible = false">取 消</el-button>
                <el-button type="primary" @click="sureAgentPay">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="批量用户清账" v-dialogDrag center :visible.sync="batchpayVisible" width="600px">
            <el-form ref="batchpayform" size="mini" :model="batchpayform" :rules="batchpayformRules" label-width="200px">
                <el-descriptions :column="1" style="margin-bottom: 20px" border>
                    <el-descriptions-item label="价格(美元)"
                        >${{ batchpayform.totalprice }} (￥{{ batchpayform.totaltargetprice }})</el-descriptions-item
                    >

                    <el-descriptions-item label="到付金额(美元)">${{ batchpayform.totalfreightprice }} </el-descriptions-item>
                    <el-descriptions-item label="用户编码">
                        {{ batchpayform.usercode }}
                    </el-descriptions-item>
                    <el-descriptions-item label="用户姓名">
                        {{ batchpayform.username }}
                    </el-descriptions-item>
                    <el-descriptions-item label="账号余额" :span="2">
                        <template v-if="batchpayform.useraccounts && batchpayform.useraccounts.length > 0">
                            <div v-for="(item, index) in batchpayform.useraccounts" :key="index" class="FlexRowVCenter">
                                <div>{{ item.currencyname }}:</div>
                                <div>{{ item.amount }}</div>
                            </div>
                        </template>
                        <div v-else>暂无余额</div>
                    </el-descriptions-item>
                </el-descriptions>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="batchpayVisible = false">取 消</el-button>
                <el-button type="primary" @click="surebatchAppuser">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="批量代理商清账" v-dialogDrag center :visible.sync="batchpayAgentVisible" width="600px">
            <el-form ref="batchpayAgentform" size="mini" :model="batchpayAgentform" :rules="batchpayAgentformRules" label-width="200px">
                <el-descriptions :column="1" style="margin-bottom: 20px" border>
                    <el-descriptions-item label="代理商">
                        {{ batchpayAgentform.agentname }}
                    </el-descriptions-item>
                    <el-descriptions-item label="成本价格"
                        >${{ batchpayAgentform.totalagentprice }}(￥{{ batchpayAgentform.totallocalagentprice }})
                    </el-descriptions-item>

                    <el-descriptions-item label="账号余额" :span="2">
                        <template v-if="batchpayAgentform.agentaccounts && batchpayAgentform.agentaccounts.length > 0">
                            <div v-for="(item, index) in batchpayAgentform.agentaccounts" :key="index" class="FlexRowVCenter">
                                <div>{{ item.currencyname }}:</div>
                                <div>{{ item.amount }}</div>
                            </div>
                        </template>
                        <div v-else>暂无余额</div>
                    </el-descriptions-item>
                </el-descriptions>
                <el-form-item label="货币种类" prop="currencyid">
                    <el-select v-model="batchpayAgentform.currencyid" class="formInput" placeholder="选择货币种类" clearable>
                        <el-option
                            v-for="(item, index) in currencyList"
                            :key="index"
                            :label="`${item.name}(${item.code})`"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="batchpayAgentVisible = false">取 消</el-button>
                <el-button type="primary" @click="surebatchAgent">确 定</el-button>
            </span>
        </el-dialog>
        <detailDialog :visible.sync="detailVisible" :data="detailInfo" :dataType="detailType"> </detailDialog>
    </div>
</template>

<script>
import API from '../../../api/index'; //读入restfulAPI
import StringTool from '../../../utils/StringTool';
import dataTable from './dataTable';
import JSTool from '../../../utils/JSTool';
import MainTool from '../../../utils/MainTool';
import detailDialog from '../../common/dialog/detailDialog.vue';
export default {
    name: 'packageDeliverTable',
    mixins: [dataTable],
    components: {
        detailDialog
    },
    data() {
        let CheckDigit = (rule, value, callback) => {
            if (!StringTool.DigitNumber(String(value))) {
                return callback(new Error('只能为整数和保留两位小数'));
            }
            if (Number(value) < 0) {
                return callback(new Error('只能为正数'));
            }
            callback();
        };
        let CheckCascaderValue = (rule, value, callback) => {
            if (!(value instanceof Array)) {
                return callback(new Error('不合规范的参数'));
            }
            if (value.length < 2) {
                return callback(new Error('请选择代理商快递'));
            }
            callback();
        };

        return {
            searchSet: {
                code: '',
                startTime: '',
                endTime: '',
                orderStatus: '',
                ispayed: '',
                isagentpayed: '',
                userCode: '',
                userName: '',
                userMobile: '',
                sendUserName: '',
                agentId: ''
            },
            SelectedList: [],
            currencyList: [],
            agentList: [],
            orderStatusOption: {
                1: { label: '申请打包', value: 1, color: '#ffb248' },
                2: { label: '发货待确认', value: 2, color: '#339ca8' },
                3: { label: '待发货', value: 3, color: '#eb8146' },
                4: { label: '已发货', value: 4, color: '#67C23A' },
                5: { label: '已签收', value: 5, color: '#626c91' }
            },
            orderPayOption: {
                0: { label: '未支付', value: '0', color: '#eb8146' },
                1: { label: '已支付', value: '1', color: '#67C23A' }
            },
            orderAgentPayOption: {
                0: { label: '未支付', value: '0', color: '#eb8146' },
                1: { label: '已支付', value: '1', color: '#67C23A' }
            },

            payVisible: false,
            payform: {
                id: '',
                code: '',
                price: '',
                freightprice: '',
                usercode: '',
                username: '',
                useraccounts: []
            },
            payformRules: {},
            payAgentVisible: false,
            payAgentform: {
                id: '',
                code: '',
                agentid: '',
                agentname: '',
                agentprice: '',
                localagentprice: '',
                currencyid: '',
                amount: '',
                agentaccounts: []
            },
            payAgentformRules: {},
            batchpayVisible: false,
            batchpayform: {
                ids: [],
                price: '',
                freightprice: '',
                usercode: '',
                username: '',
                useraccounts: []
            },
            batchpayformRules: {},
            batchpayAgentVisible: false,
            batchpayAgentform: {
                ids: [],
                agentid: '',
                agentname: '',
                totalagentprice: '',
                totallocalagentprice: '',
                currencyid: '',
                agentaccounts: []
            },
            batchpayAgentformRules: {},
            detailVisible: false,
            detailInfo: {
                id: '',
                code: '',
                agentid: '',
                agentname: '',
                agentprice: '',
                localagentprice: '',
                price: '',
                freightprice: '',
                appusercode: '',
                username: '',
                userPay: {},
                agentPay: {}
            },
            detailType: [
                {
                    attrs: { column: 2, title: '基础信息' },
                    list: [
                        { label: '内单号', key: 'code' },
                        { label: '代理商', key: 'agentname' },
                        { label: '代理商价格$', key: 'agentprice' },
                        { label: '支付金额$', key: 'price' },
                        { label: '客户代码', key: 'appusercode' }
                    ]
                },
                {
                    attrs: { column: 2, title: '客户支付' },
                    list: [
                        { label: '支付时间', key: 'userPaypaytime' },
                        { label: '经手人', key: 'userPayusername' },
                        { label: '货币', key: 'userPaycurrencyname' },
                        { label: '金额', key: 'userPayamount' }
                    ]
                },
                {
                    attrs: { column: 2, title: '代理商支付' },
                    list: [
                        { label: '支付时间', key: 'agentPaypaytime' },
                        { label: '经手人', key: 'agentPayusername' },
                        { label: '货币', key: 'agentPaycurrencyname' },
                        { label: '金额', key: 'agentPayamount' }
                    ]
                }
            ]
        };
    },
    created() {
        this.init();
    },
    mounted() {},
    watch: {},
    methods: {
        tableRowClassName({ row, rowIndex }) {
            if (row.orderstatus == 3) {
                return 'warning-row';
            } else if (row.orderstatus == 4) {
                return 'success-row';
            }
            return '';
        },
        //页面初始
        init() {
            API.StandardDoWith(API.Currency.Get({ status: 1 }), this, {
                success: (detail) => {
                    this.currencyList = detail;
                }
            });
            API.StandardDoWith(API.Agent.Get({ status: 1 }), this, {
                success: (detail) => {
                    this.agentList = detail;
                }
            });
            this.getData();
        },

        getData() {
            this.tableLoading = true;
            API.StandardDoWith(API.OrderApplyAccount.Get(this.getQuery()), this, {
                success: (detail) => {
                    this.itemTotal = detail.count;
                    this.tableData = detail.list;
                },
                complete: () => {
                    this.tableLoading = false;
                }
            });
        },
        handlePay(index, row) {
            API.StandardDoWith(API.Appuser.Get({ userCode: row.appusercode }), this, {
                success: (detail) => {
                    this.payform = {
                        id: row.id,
                        code: row.code,
                        price: row.price,
                        targetprice: row.targetprice,
                        freightprice: row.freightprice,
                        usercode: row.appusercode,
                        username: detail.list[0].name,
                        useraccounts: detail.list[0].accounts
                    };
                    this.$nextTick(() => {
                        this.payVisible = true;
                    });
                }
            });
        },
        surePay() {
            this.$refs.payform.validate((valid) => {
                if (valid) {
                    let postParam = {
                        ids: []
                    };
                    postParam.ids[0] = this.payform.id;
                    API.StandardDoWith(API.OrderApplyAccount.Put(postParam), this, {
                        success: (detail) => {
                            this.payVisible = false;
                            this.$message.success(`成功提交用户清账`);
                            this.getData();
                        }
                    });
                } else {
                    this.$message.error(`表单验证失败，无法提交`);
                    return false;
                }
            });
        },
        handleAgentPay(index, row) {
            API.StandardDoWith(API.AgentFeeAccount.Get({ agentId: row.agentid }), this, {
                success: (detail) => {
                    this.payAgentform = {
                        id: row.id,
                        code: row.code,
                        agentid: row.agentid,
                        agentname: row.agentname,
                        agentprice: row.agentprice,
                        localagentprice: row.localagentprice,
                        agentaccounts: detail.list
                    };
                    this.$nextTick(() => {
                        this.payAgentVisible = true;
                    });
                }
            });
        },
        sureAgentPay() {
            this.$refs.payAgentform.validate((valid) => {
                if (valid) {
                    let postParam = {
                        ids: [],
                        currencyid: this.payAgentform.currencyid,
                        amount: Number(this.payAgentform.amount)
                    };
                    postParam.ids[0] = this.payAgentform.id;
                    API.StandardDoWith(API.OrderApplyAccount.Post(postParam), this, {
                        success: (detail) => {
                            this.payAgentVisible = false;
                            this.$message.success(`成功提交代理商清账`);
                            this.getData();
                        }
                    });
                } else {
                    this.$message.error(`表单验证失败，无法提交`);
                    return false;
                }
            });
        },
        handleDetail(index, row) {
            this.detailInfo = null;
            this.detailInfo = {};
            this.detailInfo.id = row.id;
            this.detailInfo.code = row.code;
            this.detailInfo.agentid = row.agentid;
            this.detailInfo.agentname = row.agentname;
            this.detailInfo.agentprice = row.agentprice;
            this.detailInfo.localagentprice = row.localagentprice;
            this.detailInfo.price = row.price;
            this.detailInfo.freightprice = row.freightprice;
            this.detailInfo.appusercode = row.appusercode;
            this.detailInfo.username = row.userName;
            if (row.userPay) {
                this.detailInfo.userPaypaytime = row.userPay.paytime;
                this.detailInfo.userPayuserid = row.userPay.userid;
                this.detailInfo.userPayusername = row.userPay.username;
                this.detailInfo.userPaycurrencyid = row.userPay.currencyid;
                this.detailInfo.userPaycurrencyname = row.userPay.currencyname;
                this.detailInfo.userPayamount = row.userPay.amount;
            }
            if (row.agentPay) {
                this.detailInfo.agentPaypaytime = row.agentPay.paytime;
                this.detailInfo.agentPayuserid = row.agentPay.userid;
                this.detailInfo.agentPayusername = row.agentPay.username;
                this.detailInfo.agentPaycurrencyid = row.agentPay.currencyid;
                this.detailInfo.agentPaycurrencyname = row.agentPay.currencyname;
                this.detailInfo.agentPayamount = row.agentPay.amount;
            }

            this.detailVisible = true;
        },
        selectionChange(val) {
            this.SelectedList = val;
        },
        batchAppuser() {
            if (this.SelectedList.length == 0) {
                this.$message.error(`未选择`);
                return false;
            }

            //检查订单状态是否可以进行用户订单清账
            if (this.SelectedList.some((row) => Number(row.orderstatus) != 3 || Number(row.ispayed) != 0)) {
                this.$message.error(`有不可进行用户清账的订单`);
                return false;
            }

            //同一个用户
            let userCode = '';
            let slectedOrderids = [];
            let totalprice = 0;
            let totaltargetprice = 0;
            let totalfreightprice = 0;

            for (let appRow of this.SelectedList) {
                slectedOrderids[slectedOrderids.length] = appRow.id;
                if (userCode == '') {
                    userCode = appRow.appusercode;
                } else if (userCode != appRow.appusercode) {
                    this.$message.error(`必须选择同一个用户`);
                    return false;
                }
                totalprice += Number(appRow.price);
                totaltargetprice += Number(appRow.targetprice);
                totalfreightprice += Number(appRow.freightprice);
            }
            //查询
            API.StandardDoWith(API.Appuser.Get({ userCode }), this, {
                success: (detail) => {
                    this.batchpayform = {
                        ids: slectedOrderids,
                        totalprice: totalprice.toFixed(2),
                        totaltargetprice: totaltargetprice.toFixed(2),
                        totalfreightprice: totalfreightprice.toFixed(2),
                        usercode: userCode,
                        username: detail.list[0].name,
                        useraccounts: detail.list[0].accounts
                    };
                    this.$nextTick(() => {
                        this.batchpayVisible = true;
                    });
                }
            });
        },

        surebatchAppuser() {
            this.$refs.batchpayform.validate((valid) => {
                if (valid) {
                    let postParam = {
                        ids: this.batchpayform.ids
                    };
                    API.StandardDoWith(API.OrderApplyAccount.Put(postParam), this, {
                        success: (detail) => {
                            this.batchpayVisible = false;
                            this.$message.success(`成功提交用户清账`);
                            this.getData();
                        }
                    });
                } else {
                    this.$message.error(`表单验证失败，无法提交`);
                    return false;
                }
            });
        },

        batchAgent() {
            if (this.SelectedList.length == 0) {
                this.$message.error(`未选择`);
                return false;
            }
            if (
                this.SelectedList.some(
                    (row) => (Number(row.orderstatus) != 4 && Number(row.orderstatus) != 5) || Number(row.isagentpayed) != 0
                )
            ) {
                this.$message.error(`有不可进行代理商清账的订单`);
                return false;
            }
            //同一个代理商
            let agentid = '';
            let slectedOrderids = [];
            let totalagentprice = 0;
            let totallocalagentprice = 0;
            for (let appRow of this.SelectedList) {
                slectedOrderids[slectedOrderids.length] = appRow.id;
                if (agentid == '') {
                    agentid = appRow.agentid;
                } else if (agentid != appRow.agentid) {
                    this.$message.error(`必须选择同一个代理商`);
                    return false;
                }
                totalagentprice += Number(appRow.agentprice);
                totallocalagentprice += Number(appRow.localagentprice);
            }
            //查询
            API.StandardDoWith(API.AgentFeeAccount.Get({ agentId: agentid }), this, {
                success: (detail) => {
                    this.batchpayAgentform = {
                        ids: slectedOrderids,
                        agentid: agentid,
                        agentname: this.SelectedList[0].agentname,
                        totalagentprice: totalagentprice.toFixed(2),
                        totallocalagentprice: totallocalagentprice.toFixed(2),
                        agentaccounts: detail.list
                    };
                    this.$nextTick(() => {
                        this.batchpayAgentVisible = true;
                    });
                }
            });
        },
        surebatchAgent() {
            this.$refs.batchpayAgentform.validate((valid) => {
                if (valid) {
                    let postParam = {
                        ids: this.batchpayAgentform.ids,
                        currencyid: this.batchpayAgentform.currencyid
                    };
                    API.StandardDoWith(API.OrderApplyAccount.Post(postParam), this, {
                        success: (detail) => {
                            this.batchpayAgentVisible = false;
                            this.$message.success(`成功提交代理商清账`);
                            this.getData();
                        }
                    });
                } else {
                    this.$message.error(`表单验证失败，无法提交`);
                    return false;
                }
            });
        },
        handleExport() {
            API.StandardDownFile(API.OrderApplyAccountExport(), '订单清账数据.xlsx');
        }
    }
};
</script>

<style scoped>
@import '../../../assets/css/dataTable.css';

.RowLineSpaceBetween {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-self: center;
}
.RowCell {
    flex: 1;
}
.Dialog_Desc_Item {
    margin: 3px 10px;
}
.Dialog_Desc_Package {
    border-top: #e4e7ed 2px dotted;
}
.Dialog_Desc_Item_Image {
    height: 80px;
    width: 80px;
    margin: 5px 5px;
    border: 2px #ebeef5 solid;
    border-radius: 10px;
    overflow: hidden;
}
</style>
